package com.example.lenovo.calculate;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView textview;
    private String text = "";
    private boolean eqStaus = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn0 = (Button)findViewById(R.id.b0);
        Button btn1 = (Button)findViewById(R.id.b1);
        Button btn2 = (Button)findViewById(R.id.b2);
        Button btn3 = (Button)findViewById(R.id.b3);
        Button btn4 = (Button)findViewById(R.id.b4);
        Button btn5 = (Button)findViewById(R.id.b5);
        Button btn6 = (Button)findViewById(R.id.b6);
        Button btn7 = (Button)findViewById(R.id.b7);
        Button btn8 = (Button)findViewById(R.id.b8);
        Button btn9 = (Button)findViewById(R.id.b9);
        Button btnBack = (Button)findViewById(R.id.bBack);
        Button btnAdd = (Button)findViewById(R.id.bAdd);
        Button btnSub = (Button)findViewById(R.id.bSub);
        Button btnPoint = (Button)findViewById(R.id.bPoint);
        Button btnLeft = (Button)findViewById(R.id.bLeft);
        Button btnRight = (Button)findViewById(R.id.bRight);
        Button btnDivision = (Button)findViewById(R.id.bDivision);
        Button btnMul = (Button)findViewById(R.id.bMul);
        Button btnEqual = (Button)findViewById(R.id.bEqual);
        textview = (TextView)findViewById(R.id.textView);
        textview.requestFocus();

        btn0.setTag(0);
        btn1.setTag(1);
        btn2.setTag(2);
        btn3.setTag(3);
        btn4.setTag(4);
        btn5.setTag(5);
        btn6.setTag(6);
        btn7.setTag(7);
        btn8.setTag(8);
        btn9.setTag(9);
        btnBack.setTag(10);
        btnAdd.setTag(11);
        btnSub.setTag(12);
        btnPoint.setTag(13);
        btnLeft.setTag(14);
        btnRight.setTag(15);
        btnDivision.setTag(16);
        btnMul.setTag(17);
        btnEqual.setTag(18);

        textview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text = "";
                textview.setText(text);
            }
        });

        View.OnClickListener o1 = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int tag = (Integer)view.getTag();
                if(eqStaus){
                    text = "";
                    eqStaus = false;
                }
                switch (tag){
                    case 0:
                        text+="0";
                        break;
                    case 1:
                        text+="1";
                        break;
                    case 2:
                        text+="2";
                        break;
                    case 3:
                        text+="3";
                        break;
                    case 4:
                        text+="4";
                        break;
                    case 5:
                        text+="5";
                        break;
                    case 6:
                        text+="6";
                        break;
                    case 7:
                        text+="7";
                        break;
                    case 8:
                        text+="8";
                        break;
                    case 9:
                        text+="9";
                        break;
                    case 10:
                        text = text.substring(0,text.length()-1);
                        break;
                    case 11:
                        text+="+";
                        break;
                    case 12:
                        text+="-";
                        break;
                    case 13:
                        text+=".";
                        break;
                    case 14:
                        text+="(";
                        break;
                    case 15:
                        text+=")";
                        break;
                    case 16:
                        text+="/";
                        break;
                    case 17:
                        text+="*";
                        break;
                    case 18:
                        InfixToSuffix infixToSuffix = new InfixToSuffix();
                        String answer = infixToSuffix.Storage(text);
                        eqStaus = true;
                        text +="\n="+answer;
                        break;
                }
                textview.setText(text);
            }
        };

        btn0.setOnClickListener(o1);
        btn1.setOnClickListener(o1);
        btn2.setOnClickListener(o1);
        btn3.setOnClickListener(o1);
        btn4.setOnClickListener(o1);
        btn5.setOnClickListener(o1);
        btn6.setOnClickListener(o1);
        btn7.setOnClickListener(o1);
        btn8.setOnClickListener(o1);
        btn9.setOnClickListener(o1);
        btnBack.setOnClickListener(o1);
        btnAdd.setOnClickListener(o1);
        btnSub.setOnClickListener(o1);
        btnPoint.setOnClickListener(o1);
        btnLeft.setOnClickListener(o1);
        btnRight.setOnClickListener(o1);
        btnMul.setOnClickListener(o1);
        btnEqual.setOnClickListener(o1);
    }
}
